package com.zkingsoft.services.distribution.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zkingsoft.dao.distribution.DisMembersDao;
import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.model.distribution.DisMembers;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.distribution.DisMembersService;
import com.zkingsoft.sqlUtil.ServiceUtil;
import com.zkingsoft.util.ModelUtils;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-06-26 07:15
 */
@Service("disMembersService")
public class DisMembersServiceImpl implements DisMembersService {

	@Autowired
	private DisMembersDao disMembersDao;

	@Autowired
	private ServiceUtil serviceUtil;
	
	@Override
	public int add(DisMembers disMembers) {
		if (serviceUtil.addCheckRepeat("dis_members", "mb_tel", disMembers.getMbTel())) {
			throw new GlobleException("手机号已经被占用");
		}
		return disMembersDao.insert(disMembers);

	}

	@Override
	public int batchAdd(List<DisMembers> disMembersList) {
		int num = 0;

		int c = 10000;
		int size = disMembersList.size() / c + 1;
		for (int i = 0; i < size; i++) {
			int begin = i * c;
			int end = (i + 1) * c;
			end = end >= disMembersList.size() ? disMembersList.size() : end;
			List<DisMembers> insertList = disMembersList.subList(begin, end);
			num += disMembersDao.batchInsert(insertList);
		}
		return num;

	}

	@Override
	public int modifyByMap(DisMembers oldDisMembers, DisMembers newDisMembers) {

		if (!ModelUtils.isModified(oldDisMembers, newDisMembers)) {
			return 1;
		}
		Map<String, Object> modifyMap = null;
		try {
			modifyMap = ModelUtils
					.comparePojo2Map(oldDisMembers, newDisMembers);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("mbId", oldDisMembers.getMbId());
			disMembersDao.updateByMap(modifyMap);
		}
		return 1;
	}

	@Override
	public int modifyByModel(DisMembers disMembers) {

		return disMembersDao.updateByModel(disMembers);

	}

	@Override
	public int remove(List<Long> list) {

		return disMembersDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long mbId) {

		return disMembersDao.deleteById(mbId);

	}

	@Override
	public int removeByModel(DisMembers disMembers) {

		return disMembersDao.deleteByModel(disMembers);

	}

	@Override
	public List<DisMembers> findInPage(DisMembers disMembers,
			PaginationVO pageVo) {

		return disMembersDao.selectInPage(disMembers, pageVo);

	}

	@Override
	public List<DisMembers> findByModel(DisMembers disMembers) {

		return disMembersDao.selectByModel(disMembers);

	}

	@Override
	public int findTotal(DisMembers disMembers) {

		return disMembersDao.selectTotalRecord(disMembers);

	}

	@Override
	public DisMembers findById(Long mbId) {

		return disMembersDao.selectById(mbId);

	}
	
	@Override
	public List<DisMembers> findMyCusList(Long desId) {
		
		return disMembersDao.selectMyCusList(desId);
	}
	

}